#ifndef __Spinlock_H__
#define __Spinlock_H__

#include "libs/types.h"





struct Spinlock 
{
    uint64_t lock; //1:unlock 0:lock
};



inline void SpinlockInit(Spinlock* lock){

    lock->lock = 1;

}

void SpinlockLock(Spinlock* lock);

void SpinlockUnlock(Spinlock* lock);

int64_t SpinlockTryLock(Spinlock* lock);


#define local_irq_save(x)	__asm__ __volatile__("pushfq ; popq %0 ; cli":"=g"(x)::"memory")
#define local_irq_restore(x)	__asm__ __volatile__("pushq %0 ; popfq"::"g"(x):"memory")
#define local_irq_disable()	cli();
#define local_irq_enable()	sti();

#define spin_lock_irqsave(lock,flags)	\
do					\
{					\
	local_irq_save(flags);		\
	SpinlockLock(lock);		\
}while(0)

#define spin_unlock_irqrestore(lock,flags)	\
do						\
{						\
	SpinlockUnlock(lock);			\
	local_irq_restore(flags);		\
}while(0)

#define spin_lock_irq(lock)	\
do				\
{				\
	local_irq_disable();	\
	SpinlockLock(lock);	\
}while(0)

#define spin_unlock_irq(lock)	\
do				\
{				\
	SpinlockUnlock(lock);	\
	local_irq_enable();	\
}while(0)




#endif